﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Control.Bus;

namespace Control.Dal
{
    class DalShelf
    {
        internal static Control.Bus.BusShelf GetByID(int p)
        {
            var shs = from sh in DalCommon.Database.Shelfs
                      where sh.ID == p
                      select new BusShelf()
                      {
                          ID = sh.ID,
                          ShelfName = sh.ShelfName,
                          Tray = sh.Tray,
                          Isnormal=sh.Isnormal
                      };
            return shs.FirstOrDefault<BusShelf>();
        }

        internal static List<BusShelf> GetAll()
        {
            var shs = from sh in DalCommon.Database.Shelfs
                      select new BusShelf()
                      {
                          ID = sh.ID,
                          ShelfName = sh.ShelfName,
                          Tray = sh.Tray,
                          Isnormal=sh.Isnormal
                      };
            return shs.ToList<BusShelf>();
        }

        internal static List<BusShelf> GetListByShelfName(string shelfname)
        {
            var shs = from sh in DalCommon.Database.Shelfs
                      where sh.ShelfName==shelfname
                      select new BusShelf()
                      {
                          ID = sh.ID,
                          ShelfName = sh.ShelfName,
                          Tray = sh.Tray,
                          Isnormal=sh.Isnormal
                      };
            return shs.ToList<BusShelf>();
        }

        internal static int Insert(BusShelf busShelf)
        {
            WareHouseDataContext db = DalCommon.Database;
            Shelf sh = new Shelf()
            {
                ShelfName = busShelf.ShelfName,
                Tray = busShelf.Tray,
                Isnormal = busShelf.Isnormal
            };
            db.Shelfs.InsertOnSubmit(sh);
            db.SubmitChanges();
            return sh.ID;
        }

        internal static void Update(BusShelf busShelf)
        {
            WareHouseDataContext db = DalCommon.Database;
            Shelf sh = db.Shelfs.Single(s => s.ID == busShelf.ID);
            sh.ShelfName = busShelf.ShelfName;
            sh.Tray = busShelf.Tray;
            sh.Isnormal = busShelf.Isnormal;
            db.SubmitChanges();
        }

        internal static void Delete(BusShelf busShelf)
        {
            try
            {

                WareHouseDataContext db = DalCommon.Database;
                Shelf sh = db.Shelfs.Single(s => s.ID == busShelf.ID);
                db.Shelfs.DeleteOnSubmit(sh);
                db.SubmitChanges();
            }
            catch { }
        }
    }
}
